<?php
class Default_Model_DbTable_FoodTypes extends Default_Model_DbTable_DbTableEntityAbstract
{
	protected $_db;
	protected $_primary = 'food_type_id';
	/*
	protected $_referenceMap    = array(
        'FoodType' => array(
			'columns'       => 'food_type_id',
            'refTableClass' => 'Default_Model_DbTable_RecipeHasFoodTypes',
            'refColumns'    => 'food_type_id'
        )
    );
    */
	//protected $_dependentTables = array('Default_Model_DbTable_Roles');
	
    public function init()
    {
    	$this->_name = 'food_types';
    	$this->_db = Zend_Db_Table::getDefaultAdapter();
    }
    
    public function addFoodType($data)
    {
    	$this->insert($data);
    	return $this->getAdapter()->lastInsertId();
    }
    
    public function getFoodTypeId($foodTypeName)
    {
    	$select = $this->select()
    				   ->from($this, array('food_type_id'))
    	               ->where("food_type_name = ?", $foodTypeName);
    	               
    	$result = $this->fetchRow($select);
    	return $result;
    }
    
	public function getFoodTypes($minCount = 0)
    {
    	$select = $this->_db->select()
    	                    ->from(
    	                    	array(
    	                    		"ft"    => "food_types" 
    	                    	),
    	                    	array(
    	                    		"count"             => "COUNT(rhft.food_type_id)",
    	                    		"food_type_name"    => "ft.food_type_name",
    	                    		"food_type_latname" => "ft.food_type_latname"
    	                    	)
    	                    )
    	                    ->joinLeft(
    	                    	array('rhft' => 'recipe_has_food_type'),
    	                    	"ft.food_type_id = rhft.food_type_id"
    	                    )
    	                    ->group("ft.food_type_id")
    	                    ->having("count >= " . $minCount);
    	               
    	$result = $this->_db->fetchAll($select);
    	//$result = $select->__toString();
    	return $result;
    }
    
    public function getFoodTypeName($foodType)
    {
    	$select = $this->select()
    				   ->from($this, array('food_type_name'))
    				   ->where("food_type_latname = ?", $foodType);
    	               
    	$result = $this->fetchRow($select);
    	return $result;
    }
}